# -*- coding: utf-8 -*-
"""
TG Trader Tools 内容区域组件
"""
from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QFrame, QStackedWidget
from PySide6.QtCore import Qt

from ..base import TgComponentMixin


class TgTitleBar(QLabel, TgComponentMixin):
    """TG标题栏组件"""
    
    def __init__(self, title="欢迎使用 TG Trader Tools", parent=None):
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        
        # 先初始化父类
        QLabel.__init__(self, parent)
        TgComponentMixin.__init__(self)
        self.setText(title)
        
        # 手动调用初始化
        self.initialize()
    
    def setup_ui(self):
        """设置用户界面"""
        self.setAlignment(Qt.AlignmentFlag.AlignCenter)
    
    def apply_style(self):
        """应用样式"""
        self.setStyleSheet("""
            QLabel {
                background-color: #f8f9fa;
                padding: 15px;
                font-weight: bold;
                font-size: 16px;
                border-bottom: 1px solid #e0e0e0;
                color: #333;
            }
        """)
        self._style_applied = True
    
    def set_title(self, title):
        """设置标题"""
        self.setText(title)


class TgWelcomePage(QWidget, TgComponentMixin):
    """TG欢迎页面组件"""
    
    def __init__(self, parent=None):
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        
        # 先初始化父类
        QWidget.__init__(self, parent)
        TgComponentMixin.__init__(self)
        
        # 手动调用初始化
        self.initialize()
    
    def setup_ui(self):
        """设置用户界面"""
        layout = QVBoxLayout(self)
        
        # 欢迎标题
        title = QLabel("欢迎使用 TG Trader Tools")
        title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        title.setStyleSheet("""
            QLabel {
                font-size: 24px;
                font-weight: bold;
                color: #333;
                margin: 50px 0 20px 0;
            }
        """)
        layout.addWidget(title)
        
        # 描述文本
        desc = QLabel("请从左侧菜单选择功能")
        desc.setAlignment(Qt.AlignmentFlag.AlignCenter)
        desc.setStyleSheet("""
            QLabel {
                font-size: 14px;
                color: #666;
                margin: 0 0 50px 0;
            }
        """)
        layout.addWidget(desc)
        
        # 添加弹性空间
        layout.addStretch()
    
    def apply_style(self):
        """应用样式"""
        self.setStyleSheet("""
            QWidget {
                background-color: white;
            }
        """)
        self._style_applied = True


class TgContentArea(QFrame, TgComponentMixin):
    """TG内容区域组件"""
    
    def __init__(self, parent=None):
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        
        # 先初始化父类
        QFrame.__init__(self, parent)
        TgComponentMixin.__init__(self)
        
        # 手动调用初始化
        self.initialize()
    
    def setup_ui(self):
        """设置用户界面"""
        # 创建布局
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        
        # 创建标题栏
        self.title_bar = TgTitleBar()
        layout.addWidget(self.title_bar)
        
        # 创建堆叠部件用于显示不同的内容
        self.content_stack = QStackedWidget()
        self.content_stack.setStyleSheet("""
            QStackedWidget {
                background-color: white;
            }
        """)
        layout.addWidget(self.content_stack)
        
        # 添加默认欢迎页面
        self.welcome_page = TgWelcomePage()
        self.content_stack.addWidget(self.welcome_page)
    
    def apply_style(self):
        """应用样式"""
        self.setStyleSheet("""
            QFrame {
                background-color: white;
            }
        """)
        self._style_applied = True
    
    def set_title(self, title):
        """设置标题"""
        self.title_bar.set_title(title)
    
    def add_content_widget(self, widget):
        """添加内容部件"""
        self.content_stack.addWidget(widget)
    
    def set_current_widget(self, widget):
        """设置当前显示的部件"""
        self.content_stack.setCurrentWidget(widget)
    
    def show_welcome_page(self):
        """显示欢迎页面"""
        self.set_current_widget(self.welcome_page)
        self.set_title("欢迎使用 TG Trader Tools") 